###################################################
#Figure 4: Spending Demand By Income and Risk Type
#Author: Raluca L. Pahontu
###################################################

mydata <- read.dta13("shp.dta", convert.factors = FALSE)
mydata$rho <- factor(mydata$rho, levels=c(1,2,3))
plotdata <- mydata[which(mydata$log_household_income>9&mydata$log_household_income<13),]
fig4a <- ggplot(plotdata, aes(log_household_income)) +
  geom_density(aes(fill=as.factor(rho)), alpha=.4) +
  scale_fill_manual(values=c("black", "grey50", "grey90"), labels=c(expression(paste(rho,3)), expression(paste(rho,2)), expression(paste(rho,1))), name="Type")+
  theme_bw() + 
  theme(legend.key=element_blank(),
        legend.text=element_text(color="black", size=10),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_line(linetype = "dotted", size=.4, colour="grey90"),
        panel.grid.minor = element_line(linetype = "dotted", size=.4, colour="grey90"),
        axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 10, r = 20, b = 0, l = 0)),
        axis.ticks.length=unit(0.3,"cm"),
        axis.text = element_text(colour ="black")) + ylab("Density") + xlab("Log Income") + 
  coord_cartesian(ylim=c(0, 1), xlim=c(9,13)) +
  geom_segment(x=8.999, xend=13.001, y=-0.05, yend=-0.05, col="black")  +
  geom_segment(x=8.8, xend=8.8, y=0, yend=1, col="black") 

#and 

mydata2 <- read.dta13("margins_income.dta")
mydata2$rho=factor(mydata2$rho, levels=c(1,2,3), labels = c(expression(paste(rho,1)), expression(paste(rho,2)), expression(paste(rho,3))))

fig4b <-ggplot(data = mydata2, aes(x = as.numeric(income_distance), y = ss_hat, group = rho )) +
  geom_errorbar(aes(ymin=ss_hat-1.96*se, ymax=ss_hat+1.96*se, colour=rho), width=.05, size=.4) + 
  geom_line(aes(colour=rho),linetype="dotted",  size=0.2) + 
  geom_point(aes(colour=rho), size=1.5,  stroke=1.2) + 
  scale_colour_manual(values=c("black","grey40","grey70"),labels=c(expression(paste(rho,3)), expression(paste(rho,2)), expression(paste(rho,1))), name="Type") + 
  xlab("Income Distance from Mean") + 
  ylab("Predicted Social Spending") + 
  theme_bw() + 
  theme(legend.key=element_blank(),
        legend.text=element_text(color="black", size=10),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_line(linetype = "dotted", size=.4, colour="grey90"),
        panel.grid.minor = element_line(linetype = "dotted", size=.4, colour="grey90"),
        axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0)),
        axis.title.x = element_text(margin = margin(t = 10, r = 20, b = 0, l = 0)),
        axis.ticks.length=unit(0.3,"cm"),
        axis.text = element_text(colour ="black"))   + 
  coord_cartesian(ylim=c(0.2, .8), xlim=c(-1.5,1.5))  + 
  scale_x_continuous(breaks=c(-1.5, -1, -.5, 0, 0.5, 1, 1.5)) + 
  geom_segment(x=-1.5, xend=1.5, y=.17, yend=.17, col="black") +
  geom_segment(x=-1.65, xend=-1.65, y=0.2, yend=.8, col="black") 

library(ggpubr)
ggarrange(fig4a, fig4b, legend = "bottom")
ggsave("figure4.pdf")



